#include "gtest/gtest.h"
#include "components/thread.h"
#include "components/post.h"

using namespace Wishmaster;

class TestThread : public ::testing::Test
{
protected:
	void SetUp()
	{
		m_thread = ThreadPtr(new Thread(ThreadId("chan.ru", "/b/", 12), 0));
		m_thread->setFlag(Thread::Swapped, false);

		m_oppost = PostPtr(new Post(PostId(m_thread->id(), 12)));
		m_post1 = PostPtr(new Post(PostId(m_thread->id(), 23)));
		m_post2 = PostPtr(new Post(PostId(m_thread->id(), 34)));
		m_post3 = PostPtr(new Post(PostId(m_thread->id(), 45)));
		m_post4 = PostPtr(new Post(PostId(m_thread->id(), 56)));

		m_thread->appendPost(m_oppost);
		m_thread->appendPost(m_post1);
		m_thread->appendPost(m_post2);
		m_thread->appendPost(m_post3);
		m_thread->appendPost(m_post4);
	}

	void TearDown()
	{
		m_thread = ThreadPtr();
	}

	Wishmaster::ThreadPtr m_thread;
	Wishmaster::PostPtr m_oppost;
	Wishmaster::PostPtr m_post1;
	Wishmaster::PostPtr m_post2;
	Wishmaster::PostPtr m_post3;
	Wishmaster::PostPtr m_post4;
};

TEST_F(TestThread, PostManipulation)
{
	ASSERT_EQ(5, m_thread->getActualPostsCount());
	ASSERT_EQ(5, m_thread->getPostsCount());

	ASSERT_EQ(m_oppost.data(), m_thread->getOpPost().data());

	ASSERT_TRUE(m_oppost == m_thread->getPost(0));
	ASSERT_TRUE(m_post2 == m_thread->getPost(2));
	ASSERT_TRUE(m_post3 == m_thread->lookupPost(m_post3->id()));
	ASSERT_TRUE(m_post4 == m_thread->pickPost(m_post4->id()));
	ASSERT_EQ(4, m_thread->getPostsCount());

	m_thread->hidePost(m_post1->id());
	ASSERT_TRUE(m_thread->isPostHidden(m_post1->id()));
	ASSERT_EQ(4, m_thread->getPostsCount());
}

TEST_F(TestThread,Constructor)
{
	ThreadPtr thread = ThreadPtr(new Thread(ThreadId("chan.ru", "/b/", 42), 0));
	ASSERT_TRUE(thread->id().threadNumericId() == (unsigned long)42);
	ASSERT_TRUE(thread->flag(Thread::Alive));
	ASSERT_FALSE(thread->flag(Thread::Swapped));
	ASSERT_EQ(0, thread->getPostsCount());
}
